#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QDir>
#include <QLocale>
#include <QTranslator>
#include <QDebug>

#include "appfileprovider.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QApplication::setApplicationName("tocgen");
    QApplication::setApplicationVersion("1.1");
    QApplication::setOrganizationDomain("www.opensource.org");
    QApplication::setOrganizationName("opensource.org");

    if ( !AppFileProvider::createConfigDir() )
        qFatal("Could not create user config directory. Permission denied.");

    QString locale;
    QStringList args = app.arguments();

    int i = args.indexOf("--locale");
    if ( i < 0 )
        locale = QLocale::system().name();
    else
        locale = args.at(i+1);

    qDebug() << "Using locale: " << locale;

    QString translation_file;
    translation_file = AppFileProvider::getConfigFile(QString("toc.%1.qm").arg(locale));

    QTranslator app_translator;
    if ( app_translator.load(translation_file.remove(".qm")) )
    {
        app.installTranslator(&app_translator);
        qDebug() << "Installed app translator from: " << translation_file;
    }
    else
        qDebug() << "No translation file for this locale: " << locale << " (" << translation_file << ")";

    MainWindow w;
    w.show();

    return app.exec();
}
